package com.ctvit.sjk.knowledge.app.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ctvit.sjk.knowledge.app.pojo.domain.TabAppAccessToken;
import com.ctvit.sjk.knowledge.app.pojo.dto.AppAccessTokenDTO;
import com.ctvit.sjk.knowledge.app.pojo.dto.AppAuthenticationDTO;
import com.ctvit.sjk.knowledge.app.pojo.vo.AppAccessTokenGetVO;
import com.ctvit.sjk.knowledge.app.service.ITabAppAccessTokenService;
import com.ctvit.sjk.knowledge.core.controller.KnowledgeController;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 应用访问令牌Controller
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@Slf4j
@RestController
@RequestMapping("knowledge/application")
public class TabAppAccessTokenController extends KnowledgeController {

    private final ITabAppAccessTokenService tabAppAccessTokenService;

    public TabAppAccessTokenController(ITabAppAccessTokenService tabAppAccessTokenService) {
        this.tabAppAccessTokenService = tabAppAccessTokenService;
    }

    /**
     * 获取应用访问令牌
     */
    @GetMapping("{appId}/access_token")
    public AjaxResult get(@PathVariable String appId) {
        return success(new AppAccessTokenGetVO(tabAppAccessTokenService.getById(appId)));
    }

    /**
     * 更新应用访问令牌
     */
    @PutMapping("{appId}/access_token")
    public AjaxResult update(@PathVariable String appId, @RequestBody AppAccessTokenDTO dto) {
        LambdaUpdateWrapper<TabAppAccessToken> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Objects.nonNull(dto.getAccessNum()), TabAppAccessToken::getAccessNum, dto.getAccessNum());
        wrapper.set(Objects.nonNull(dto.getWhiteActive()), TabAppAccessToken::getWhiteActive, dto.getWhiteActive());
        wrapper.set(Objects.nonNull(dto.getWhiteList()), TabAppAccessToken::getWhiteList, JSON.toJSONString(dto.getWhiteList()));
        wrapper.set(Objects.nonNull(dto.getShowSource()), TabAppAccessToken::getShowSource, dto.getShowSource());
        wrapper.set(Objects.nonNull(dto.getLanguage()), TabAppAccessToken::getLanguage, dto.getLanguage());
        wrapper.set(TabAppAccessToken::getUpdateId, CREATE_ID);
        wrapper.set(TabAppAccessToken::getUpdateTime, LocalDateTime.now());
        wrapper.eq(TabAppAccessToken::getAppId, appId);
        tabAppAccessTokenService.update(wrapper);
        return success(new AppAccessTokenGetVO(tabAppAccessTokenService.getById(appId)));
    }

    /**
     * 应用认证
     */
    @PostMapping("authentication")
    public AjaxResult authentication(@RequestBody AppAuthenticationDTO dto) {
        return toAjax(createToken(tabAppAccessTokenService.authentication(dto)));
    }

}
